import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void LogIn (){
        ArrayList<Student> list = new ArrayList<>();
        while (true) {
            System.out.println("----------------------欢迎使用学生管理系统----------------------");
            System.out.println("1：添加学生");
            System.out.println("2：删除学生");
            System.out.println("3：修改学生");
            System.out.println("4：查询学生");
            System.out.println("5：退出系统");
            System.out.println("请输入你的选择：");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case "1" -> addStudent(list);
                case "2" -> deleteStudent(list);
                case "3" -> updateStudent(list);
                case "4" -> queryStudent(list);
                case "5" -> {
                    System.out.println("退出");
                    //break loop;
                    System.exit(0);//停止虚拟机运行
                }
                default->
                    System.out.println("你输入的格式不对请重新输入");
            }
        }


    }

    //4：查询学生
    private static void queryStudent(ArrayList<Student> list) {
        Student s = new Student();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入查询学生的ID");
            String id = sc.next();
            if (search(list,id)){
                //输出学生信息
                System.out.println("id\t\t姓名\t年龄\t地址");
                    Student stu = list.get(getIndex(list,id));
                    System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());



            }else {
                System.out.println("当前无该学生信息，请添加后再查询");
                break;
            }
            break;
        }


    }

    /*3：修改学生*/
    private static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需要修改的学生id");
        String id = sc.next();
        if (search(list,id)){
        }else {
            System.out.println("你输入的id不存在");
            return;
        }

        Student stu = list.get(getIndex(list,id));

        System.out.println("请输入姓名");
        String name = sc.next();
        stu.setName(name);

        System.out.println("请输入年龄");
        int age = sc.nextInt();
        stu.setAge(age);

        System.out.println("请输入地址");
        String address = sc.next();
        stu.setAddress(address);

        System.out.println("成功修改信息");



        
    }

    //2：删除学生
    private static void deleteStudent(ArrayList<Student> list) {
        Student stu = new Student();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需要删除的学生id");
        String id = sc.next();
        if (search(list,id)){
            list.remove(getIndex(list,id));
            System.out.println("id为：" + id + "的学生删除成功");
        }else System.out.println("id不存在，删除失败");
        
    }

    //1：添加学生
    private static void addStudent(ArrayList<Student> list) {
        Student s = new Student();
        Scanner sc = new Scanner(System.in);
        String id = null;
        while (true) {
            System.out.println("请输入ID");
            id = sc.next();
            if (search(list,id)){
                System.out.println("你输入的id已经被占用");
                System.out.println("请重新输入");
            }else {
                s.setId(id);  //记得添加id啊
                System.out.println("创建新成员");
                break;
            }
        }

        System.out.println("请输入姓名");
        String name = sc.next();
        s.setName(name);

        System.out.println("请输入年龄");
        int age = sc.nextInt();
        s.setAge(age);

        System.out.println("请输入地址");
        String address = sc.next();
        s.setAddress(address);


        list.add(s);
        System.out.println("成功创建信息");

    }

    //查询id是否在里面，在返回true，不在返回false
    private static boolean search(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String stuID = stu.getId();
            if (id.equals(stuID)){
                //在返回true
                return true;
            }
        }
        //不在返回false
        return false;
    }

    //获取数据对应索引
    private static int getIndex(ArrayList<Student> list, String id){
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String stuID = stu.getId();
            if (id.equals(stuID)){
                return i;
            }

        }
        return -1;
    }

}
